﻿namespace CompanyPayRollSystem
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using CompanyPayRollSystem.BusinessLayer;
    using CompanyPayRollSystem.Database;

    /// <summary>
    /// UI Form that calculates employee salary
    /// </summary>
    public partial class CalculateSalaryForm : Form
    {
        /// <summary>
        /// Initializes a new instance of the CalculateSalaryForm class.
        /// </summary>
        public CalculateSalaryForm()
        {
            InitializeComponent();
        }

        #region Event Handlers

        /// <summary>
        /// Handles the Click event of the btnCalculateSalary control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void btnCalculateSalary_Click(object sender, EventArgs e)
        {
            if (this.cmbEmployees.SelectedValue == null)
            {
                MessageBox.Show("Select an employee first");
                return;
            }

            this.CalculateSalaryItems();
        }

        /// <summary>
        /// Handles the SelectedIndexChanged event of the cmbDepartments control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void cmbDepartments_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.LoadEmployees();
        }

        /// <summary>
        /// Handles the SelectedIndexChanged event of the cmbJobLevels control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void cmbJobLevels_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.LoadEmployees();
        }

        /// <summary>
        /// Handles the Load event of the CalculateSalaryForm control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void CalculateSalaryForm_Load(object sender, EventArgs e)
        {
            this.LoadDepartments();
            this.LoadEmployees();
            this.LoadMonths();
            this.LoadYears();
            this.LoadJobLevels();
        }
        #endregion

        #region Load Comboboxes

        /// <summary>
        /// Loads the departments.
        /// </summary>       
        private void LoadDepartments()
        {
            List<Department> departments = DepartmentManager.GetDepartments();
            this.cmbDepartments.Items.Add("All");
            foreach (Department department in departments)
            {
                this.cmbDepartments.Items.Add(department);
            }

            this.cmbDepartments.DisplayMember = ColumnsNames.DepartmentColumnsNames.DepartmentName;
            this.cmbDepartments.ValueMember = ColumnsNames.DepartmentColumnsNames.DepartmentID;
        }

        

        /// <summary>
        /// Loads the employees.
        /// </summary>       
        private void LoadEmployees()
        {
            this.cmbEmployees.DataSource = null;
            int? departmentID = null;
            int? jobLevelID = null;
            if (this.cmbDepartments.SelectedIndex > 0)
            {
                departmentID = ((Department)this.cmbDepartments.SelectedItem).DepartmentID;
            }

            if (this.cmbJobLevels.SelectedIndex > 0)
            {
                jobLevelID = ((JobLevel)this.cmbJobLevels.SelectedItem).JobLevelID;
            }

            List<Employee> employees = EmployeeManager.GetEmployees(departmentID, jobLevelID);
            this.cmbEmployees.DataSource = employees;
            this.cmbEmployees.DisplayMember = ColumnsNames.EmployeeColumnsNames.EmployeeName;
            this.cmbEmployees.ValueMember = ColumnsNames.EmployeeColumnsNames.EmployeeID;

        }

        /// <summary>
        /// Loads the months.
        /// </summary>       
        private void LoadMonths()
        {
            string[] months = System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.MonthNames;
            for (int i = 0; i < 12; i++)
            {
                this.cmbMonth.Items.Add(months[i]);
            }

            this.cmbMonth.SelectedIndex = DateTime.Now.Month - 1;
        }

        /// <summary>
        /// Loads the years.
        /// </summary>       
        private void LoadYears()
        {
            for (int i = DateTime.Now.Year - 3; i <= DateTime.Now.Year; i++)
            {
                this.cmbYear.Items.Add(i);
            }

            this.cmbYear.SelectedIndex = this.cmbYear.Items.Count - 1;
        }

        /// <summary>
        /// Loads job levels
        /// </summary>
        private void LoadJobLevels()
        {
            List<JobLevel> jobLevels = JobLevelManager.GetJobLevels();
            this.cmbJobLevels.Items.Add("All");
            foreach (JobLevel jobLevel in jobLevels)
            {
                this.cmbJobLevels.Items.Add(jobLevel);
            }

            this.cmbJobLevels.DisplayMember = ColumnsNames.JobLevelColumnsNames.JobLevelName;
            this.cmbJobLevels.ValueMember = ColumnsNames.JobLevelColumnsNames.JobLevelID;
        }

        #endregion

        #region Business
        /// <summary>
        /// Calculates the salary items.
        /// </summary>
        private void CalculateSalaryItems()
        {
            EmployeeSalaryItem employeeSalaryItem = EmployeeSalaryItemsManager.GetEmployeeSalaryItems((int)this.cmbEmployees.SelectedValue, (int)this.cmbMonth.SelectedIndex + 1, (int)this.cmbYear.SelectedItem);
            if (employeeSalaryItem != null)
            {
                SalaryItemsForm salaryItemsForm = new SalaryItemsForm(employeeSalaryItem);
                salaryItemsForm.ShowDialog();
            }
            else
            {
                MessageBox.Show("Error");
            }
        }
        #endregion
    }
}
